package com.mm.controller;

import com.mm.dds.datasource.DataSourceContextHolder;
import com.mm.jwt.JWTUtil;
import com.mm.shiro.dao.User;
import com.mm.shiro.service.UserService;
import com.mm.utils.ResponseBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import static com.mm.jwt.JWTUtil.getUsername;

/**
 * Created by Administrator on 2018/9/17/017.
 */
@RestController
public class WebController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseBean login(
            @RequestParam("password") String password,
            @RequestParam("username") String username) {
        DataSourceContextHolder.setDBType("default");
        User user = userService.findUserByName(username);
        if (user.getPassword().equals(password)) {

            return new ResponseBean(200, "Login success", JWTUtil.sign(username, password));
        } else {
            throw new UnauthorizedException();
        }
    }

    @GetMapping("/user/info")
    public ResponseBean userinfo() {
        Subject subject = SecurityUtils.getSubject();
        String token = (String) subject.getPrincipal();
        String username = getUsername(token);
        User user = userService.findUserByName(username);
        user.setPassword("你是看不到的");
        return new ResponseBean(200, "获取用户成功", user);
    }

    @GetMapping("/article")
    public ResponseBean article() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            return new ResponseBean(200, "You are already logged in", null);
        } else {
            return new ResponseBean(200, "You are guest", null);
        }
    }

    @GetMapping("/add")
    public ResponseBean add() {
        Subject subject = SecurityUtils.getSubject();
        User user = userService.findUserByName(getUsername((String) subject.getPrincipal()));
        subject.checkPermission("add");
        subject.checkPermission("adds");
        //角色
        if (subject.hasRole("admin")) {

            return new ResponseBean(200, "有权限", null);

        } else {

            return new ResponseBean(200, "无权限", null);

        }

    }

    @GetMapping("/adds")
    public ResponseBean adds() {
        Subject subject = SecurityUtils.getSubject();

        return new ResponseBean(200, "adds", null);

    }

    @GetMapping("/require_auth")
    @RequiresAuthentication
    public ResponseBean requireAuth() {
        return new ResponseBean(200, "You are authenticated", null);
    }

    @GetMapping("/require_role")
    @RequiresRoles("admin")
    public ResponseBean requireRole() {
        return new ResponseBean(200, "You are visiting require_role", null);
    }

    @GetMapping("/require_permission")
    @RequiresPermissions(logical = Logical.AND, value = {"view", "edit"})
    public ResponseBean requirePermission() {
        return new ResponseBean(200, "You are visiting permission require edit,view", null);
    }

    @RequestMapping("/logout")
    public ResponseBean logOut() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
//        session.removeAttribute("user");
        return new ResponseBean(200, "退出成功", null);
    }

    @RequestMapping(path = "/401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseBean unauthorized() {
        return new ResponseBean(401, "Unauthorized", null);
    }
}
